﻿using System;
using System.Collections.Generic;
using Telerik.Web.UI;
using Newtonsoft.Json;
using System.Data;
using System.Web.UI.WebControls;

namespace RN.ERP.Web.Person
{
    public partial class PersonEmpStatisticOldExpert : BasePage
    {
        protected string AreaCode
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }
        
        /// <summary>
        /// 作为查询地区限定条件
        /// </summary>
        protected string AreaCodeWhere
        {
            get
            {
                return UserInfo.AreaCode;
            }
        }
        
        /// <summary>
        /// 保存人员类型
        /// </summary>
        private Dictionary<string, string> dict
        {
            get
            {
                Dictionary<string, string> flowTypeDic = new Dictionary<string, string>();
                ////flowTypeDic.Add("DBName", "分库名称");
                ////flowTypeDic.Add("DBType", "分库");
                flowTypeDic.Add("FlowType11", "总数");      ////专家 
                flowTypeDic.Add("FlowType12", "待确定");    ////专家 
                flowTypeDic.Add("FlowType13", "待上报");    ////专家 
                flowTypeDic.Add("FlowType14", "已上报");    ////专家 
                flowTypeDic.Add("FlowType21", "待审核");    ////县级 
                flowTypeDic.Add("FlowType22", "通过");      ////县级 
                flowTypeDic.Add("FlowType23", "退回");      ////县级 
                flowTypeDic.Add("FlowType31", "待审核");    ////市级 
                flowTypeDic.Add("FlowType32", "通过");      ////市级 
                flowTypeDic.Add("FlowType33", "退回");      ////市级 
                flowTypeDic.Add("FlowType41", "待审核");    ////省级 
                flowTypeDic.Add("FlowType42", "通过");      ////省级 
                flowTypeDic.Add("FlowType43", "有条件通过");////省级 
                flowTypeDic.Add("FlowType44", "退回");      ////省级 
                return flowTypeDic;
            }
        }

        public override bool IsCheckPermission
        {
            get
            {
                return false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFlowCodes();
                //绑定列表
                rgPersonList.DataSource = ToDataTable();
            }
        }

        /// <summary>
        /// 取得审核状态绑定查询选项
        /// </summary>
        private void BindFlowCodes()
        {
            using (BasicService.BasicServiceClient client = new BasicService.BasicServiceClient())
            {
                string msg = string.Empty;
                var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(client.GetAllStatusFlowCodesByTitle(out msg, UserInfo.RoleId, AreaCode, ""));
                rblFlowCodeList.DataSource = dic;
                rblFlowCodeList.DataTextField = "key";
                rblFlowCodeList.DataValueField = "value";
                rblFlowCodeList.DataBind();
            }
        }
        
        /// <summary>
        /// 格式化结果集
        /// </summary>
        protected void rgPersonList_ItemDataBound(object sender, GridItemEventArgs e)
        {
            int gridCount = 0;
            if (e.Item is GridDataItem)
            {
                GridDataItem gridItem = e.Item as GridDataItem;
                ///根据人员类型列表取得相应列并将统计数目大于零的加链接
                foreach (var d in dict)
                {
                    if (gridItem[d.Key] != null)
                    {
                        int.TryParse(gridItem[d.Key].Text, out gridCount);
                        string dbType = gridItem["DBType"].Text;
                        if (dbType.ToLower() == "&nbsp;")
                        {
                            dbType = "";
                        }

                        if (gridCount > 0 && d.Key != "FlowType11")
                        {
                            if (d.Key == "FlowType12")
                            {
                                gridItem[d.Key].Text = "<a href='../Person/ExpertForOld.aspx?type=0&areacode=" + dbType + "'><span style='color: #43a9f5'>" + gridCount.ToString() + "</span></a>";
                            }
                            else
                            {
                                gridItem[d.Key].Text = "<a href='../person/PersonExpertList.aspx?isOldExpert=true&areaCode=" + dbType + "&flowCode=" + d.Key + "'><span style='color: #43a9f5'>" + gridCount.ToString() + "</span></a>";
                            }
                        }
                        else
                        {
                            gridItem[d.Key].Text = gridCount.ToString();
                        }
                    }
                }
                
            }
        }
        
        /// <summary>
        /// 审核状态查询
        /// </summary>
        protected void rblFlowCodeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            rgPersonList.DataSource = ToDataTable();
            rgPersonList.DataBind();
        }

        /// <summary>
        /// 取得数据源并将数据源转化成DataTable
        /// </summary>
        public DataTable ToDataTable()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                //取得统计数据结果集
                var statist = JsonConvert.DeserializeObject<ModelService.StatisticsStruct>(client.GetPersonExpertStatistic(8065, UserInfo.LocalArea, 3));
                //转化成DataTable
                DataTable result = UtilitiesTool.StructToDataTable(statist);
                if (result == null)
                {
                    return result;
                }
                
                return result;
            }
        }
    }
}